<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>设备管理 - Modbus监控系统</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            padding: 20px;
        }

        .container {
            max-width: 1400px;
            margin: 0 auto;
        }

        .header {
            background: white;
            padding: 20px 30px;
            border-radius: 10px;
            margin-bottom: 20px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }

        .header h1 {
            font-size: 24px;
            color: #333;
            margin-bottom: 20px;
        }

        .toolbar {
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .toolbar-left {
            display: flex;
            gap: 10px;
        }

        .btn {
            padding: 10px 20px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 14px;
            font-weight: 500;
            transition: all 0.3s;
            display: inline-flex;
            align-items: center;
            gap: 8px;
        }

        .btn-primary {
            background: #3b82f6;
            color: white;
        }

        .btn-primary:hover {
            background: #2563eb;
        }

        .btn-success {
            background: #10b981;
            color: white;
        }

        .btn-success:hover {
            background: #059669;
        }

        .btn-danger {
            background: #ef4444;
            color: white;
        }

        .btn-danger:hover {
            background: #dc2626;
        }

        .btn-secondary {
            background: #6b7280;
            color: white;
        }

        .btn-secondary:hover {
            background: #4b5563;
        }

        .status-summary {
            font-size: 16px;
            color: #333;
            font-weight: 500;
        }

        .status-summary .online {
            color: #10b981;
            font-weight: bold;
        }

        .status-summary .offline {
            color: #ef4444;
        }

        .device-list {
            background: white;
            border-radius: 10px;
            padding: 20px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }

        .device-item {
            background: #fafafa;
            border: 2px solid #e5e7eb;
            border-radius: 8px;
            padding: 20px;
            margin-bottom: 15px;
            display: flex;
            align-items: center;
            gap: 20px;
            transition: all 0.3s;
        }

        .device-item:hover {
            border-color: #3b82f6;
            box-shadow: 0 2px 8px rgba(59, 130, 246, 0.2);
        }

        .device-item.online {
            background: #f0fdf4;
            border-color: #10b981;
        }

        .device-status {
            width: 24px;
            height: 24px;
            border-radius: 50%;
            flex-shrink: 0;
        }

        .device-status.online {
            background: #10b981;
            box-shadow: 0 0 10px rgba(16, 185, 129, 0.5);
            animation: pulse 2s infinite;
        }

        .device-status.offline {
            background: #ef4444;
        }

        @keyframes pulse {

            0%,
            100% {
                opacity: 1;
            }

            50% {
                opacity: 0.6;
            }
        }

        .device-info {
            flex: 1;
        }

        .device-name {
            font-size: 20px;
            font-weight: bold;
            color: #333;
            margin-bottom: 8px;
        }

        .device-details {
            color: #666;
            font-size: 14px;
            display: flex;
            gap: 20px;
        }

        .device-actions {
            display: flex;
            gap: 10px;
        }

        .icon-btn {
            padding: 8px 16px;
            border: 1px solid #ddd;
            background: white;
            border-radius: 5px;
            cursor: pointer;
            font-size: 14px;
            transition: all 0.3s;
        }

        .icon-btn:hover {
            background: #f3f4f6;
        }

        .icon-btn.view {
            color: #3b82f6;
            border-color: #3b82f6;
        }

        .icon-btn.view:hover {
            background: #eff6ff;
        }

        .icon-btn.edit {
            color: #f59e0b;
            border-color: #f59e0b;
        }

        .icon-btn.edit:hover {
            background: #fffbeb;
        }

        .icon-btn.delete {
            color: #ef4444;
            border-color: #ef4444;
        }

        .icon-btn.delete:hover {
            background: #fef2f2;
        }

        /* 对话框样式 */
        .modal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0, 0, 0, 0.5);
            z-index: 1000;
            align-items: center;
            justify-content: center;
        }

        .modal.active {
            display: flex;
        }

        .modal-content {
            background: white;
            border-radius: 10px;
            padding: 30px;
            max-width: 500px;
            width: 90%;
            box-shadow: 0 10px 25px rgba(0, 0, 0, 0.2);
        }

        .modal-header {
            font-size: 20px;
            font-weight: bold;
            margin-bottom: 20px;
            color: #333;
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            color: #666;
            font-size: 14px;
        }

        .form-group input {
            width: 100%;
            padding: 10px;
            border: 2px solid #e5e7eb;
            border-radius: 5px;
            font-size: 14px;
        }

        .form-group input:focus {
            outline: none;
            border-color: #3b82f6;
        }

        .modal-actions {
            display: flex;
            gap: 10px;
            justify-content: flex-end;
            margin-top: 25px;
        }

        .empty-state {
            text-align: center;
            padding: 60px 20px;
            color: #9ca3af;
        }

        .empty-state-icon {
            font-size: 64px;
            margin-bottom: 20px;
        }
    </style>
</head>

<body>
    <div class="container">
        <!-- 顶部工具栏 -->
        <div class="header">
            <h1>🖥️ 设备管理</h1>
            <div class="toolbar">
                <div class="toolbar-left">
                    <button class="btn btn-primary" onclick="openAddDialog()">
                        ➕ 添加设备
                    </button>
                    <button class="btn btn-success" onclick="connectAll()">
                        🔗 全部连接
                    </button>
                    <button class="btn btn-danger" onclick="disconnectAll()">
                        ⛔ 全部断开
                    </button>
                </div>
                <div class="status-summary">
                    在线: <span class="online">2</span> / <span>3</span>
                </div>
            </div>
        </div>

        <!-- 设备列表 -->
        <div class="device-list">
            <!-- 设备 1 - 在线 -->
            <div class="device-item online">
                <div class="device-status online"></div>
                <div class="device-info">
                    <div class="device-name">PLC_01</div>
                    <div class="device-details">
                        <span>📍 192.168.1.100:502</span>
                        <span>⏱️ 已连接 2小时15分</span>
                        <span>📊 已采集 7,890 条数据</span>
                    </div>
                </div>
                <div class="device-actions">
                    <button class="btn btn-secondary" onclick="disconnect(1)">断开连接</button>
                    <button class="icon-btn view" onclick="viewData(1)">📊 查看数据</button>
                    <button class="icon-btn edit" onclick="editDevice(1)">✏️ 编辑</button>
                    <button class="icon-btn delete" onclick="deleteDevice(1)">🗑️ 删除</button>
                </div>
            </div>

            <!-- 设备 2 - 在线 -->
            <div class="device-item online">
                <div class="device-status online"></div>
                <div class="device-info">
                    <div class="device-name">PLC_02</div>
                    <div class="device-details">
                        <span>📍 192.168.1.101:502</span>
                        <span>⏱️ 已连接 1小时32分</span>
                        <span>📊 已采集 5,520 条数据</span>
                    </div>
                </div>
                <div class="device-actions">
                    <button class="btn btn-secondary" onclick="disconnect(2)">断开连接</button>
                    <button class="icon-btn view" onclick="viewData(2)">📊 查看数据</button>
                    <button class="icon-btn edit" onclick="editDevice(2)">✏️ 编辑</button>
                    <button class="icon-btn delete" onclick="deleteDevice(2)">🗑️ 删除</button>
                </div>
            </div>

            <!-- 设备 3 - 离线 -->
            <div class="device-item">
                <div class="device-status offline"></div>
                <div class="device-info">
                    <div class="device-name">PLC_03</div>
                    <div class="device-details">
                        <span>📍 192.168.1.102:502</span>
                        <span>⏱️ 离线</span>
                        <span>📊 历史数据 3,200 条</span>
                    </div>
                </div>
                <div class="device-actions">
                    <button class="btn btn-success" onclick="connect(3)">连接设备</button>
                    <button class="icon-btn view" onclick="viewData(3)">📊 查看数据</button>
                    <button class="icon-btn edit" onclick="editDevice(3)">✏️ 编辑</button>
                    <button class="icon-btn delete" onclick="deleteDevice(3)">🗑️ 删除</button>
                </div>
            </div>
        </div>
    </div>

    <!-- 添加设备对话框 -->
    <div class="modal" id="addDeviceModal">
        <div class="modal-content">
            <div class="modal-header">➕ 添加新设备</div>
            <form>
                <div class="form-group">
                    <label>设备名称</label>
                    <input type="text" placeholder="例如：PLC_04" id="deviceName">
                </div>
                <div class="form-group">
                    <label>IP 地址</label>
                    <input type="text" placeholder="例如：192.168.1.103" id="deviceIp">
                </div>
                <div class="form-group">
                    <label>端口</label>
                    <input type="number" value="502" id="devicePort">
                </div>
                <div class="form-group">
                    <label>设备 ID（Modbus 从站地址）</label>
                    <input type="number" value="1" id="deviceId">
                </div>
                <div class="modal-actions">
                    <button type="button" class="btn btn-secondary" onclick="closeAddDialog()">取消</button>
                    <button type="button" class="btn btn-primary" onclick="addDevice()">添加</button>
                </div>
            </form>
        </div>
    </div>

    <!-- 编辑设备对话框 -->
    <div class="modal" id="editDeviceModal">
        <div class="modal-content">
            <div class="modal-header">✏️ 编辑设备</div>
            <form>
                <div class="form-group">
                    <label>设备名称</label>
                    <input type="text" placeholder="设备名称" id="editDeviceName">
                </div>
                <div class="form-group">
                    <label>IP 地址</label>
                    <input type="text" placeholder="IP 地址" id="editDeviceIp">
                </div>
                <div class="form-group">
                    <label>端口</label>
                    <input type="number" value="502" id="editDevicePort">
                </div>
                <div class="form-group">
                    <label>设备 ID（Modbus 从站地址）</label>
                    <input type="number" value="1" id="editDeviceId">
                </div>
                <div class="modal-actions">
                    <button type="button" class="btn btn-secondary" onclick="closeEditDialog()">取消</button>
                    <button type="button" class="btn btn-primary" onclick="saveDevice()">保存</button>
                </div>
            </form>
        </div>
    </div>

    <script>
        function openAddDialog() {
            document.getElementById('addDeviceModal').classList.add('active');
        }

        function closeAddDialog() {
            document.getElementById('addDeviceModal').classList.remove('active');
        }

        function addDevice() {
            const name = document.getElementById('deviceName').value;
            const ip = document.getElementById('deviceIp').value;
            const port = document.getElementById('devicePort').value;
            const id = document.getElementById('deviceId').value;

            if (!name || !ip) {
                alert('请填写设备名称和IP地址！');
                return;
            }

            alert(`设备添加成功！\n名称: ${name}\nIP: ${ip}:${port}\n设备ID: ${id}`);
            closeAddDialog();
        }

        function connect(deviceId) {
            alert(`正在连接设备 ${deviceId}...`);
        }

        function disconnect(deviceId) {
            if (confirm(`确定要断开设备 ${deviceId} 的连接吗？`)) {
                alert(`设备 ${deviceId} 已断开连接`);
            }
        }

        function connectAll() {
            alert('正在连接所有设备...');
        }

        function disconnectAll() {
            if (confirm('确定要断开所有设备的连接吗？')) {
                alert('所有设备已断开连接');
            }
        }

        function viewData(deviceId) {
            // 跳转到仪表盘页面，并传递设备ID参数
            window.location.href = `1_DashboardPage.html?deviceId=${deviceId}`;
        }

        function editDevice(deviceId) {
            // 模拟加载设备数据
            const devices = {
                1: { name: 'PLC_01', ip: '192.168.1.100', port: 502, id: 1 },
                2: { name: 'PLC_02', ip: '192.168.1.101', port: 502, id: 1 },
                3: { name: 'PLC_03', ip: '192.168.1.102', port: 502, id: 1 }
            };

            const device = devices[deviceId];
            if (device) {
                document.getElementById('editDeviceName').value = device.name;
                document.getElementById('editDeviceIp').value = device.ip;
                document.getElementById('editDevicePort').value = device.port;
                document.getElementById('editDeviceId').value = device.id;

                // 保存当前编辑的设备ID
                document.getElementById('editDeviceModal').dataset.deviceId = deviceId;

                openEditDialog();
            }
        }

        function openEditDialog() {
            document.getElementById('editDeviceModal').classList.add('active');
        }

        function closeEditDialog() {
            document.getElementById('editDeviceModal').classList.remove('active');
        }

        function saveDevice() {
            const deviceId = document.getElementById('editDeviceModal').dataset.deviceId;
            const name = document.getElementById('editDeviceName').value;
            const ip = document.getElementById('editDeviceIp').value;
            const port = document.getElementById('editDevicePort').value;
            const id = document.getElementById('editDeviceId').value;

            if (!name || !ip) {
                alert('请填写设备名称和IP地址！');
                return;
            }

            alert(`设备 ${deviceId} 更新成功！\n名称: ${name}\nIP: ${ip}:${port}\n设备ID: ${id}`);
            closeEditDialog();
        }

        function deleteDevice(deviceId) {
            if (confirm(`确定要删除设备 ${deviceId} 吗？此操作不可恢复！`)) {
                alert(`设备 ${deviceId} 已删除`);
            }
        }

        // 关闭对话框（点击背景）
        document.getElementById('addDeviceModal').addEventListener('click', function (e) {
            if (e.target === this) {
                closeAddDialog();
            }
        });

        document.getElementById('editDeviceModal').addEventListener('click', function (e) {
            if (e.target === this) {
                closeEditDialog();
            }
        });
    </script>
</body>

</html>